package com.example.demo.controller;

import com.example.demo.dao.UserInfo;
import com.example.demo.service.UserService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan("com.example.demo")
@SpringBootApplication
public class IocDemoApplication {

    public static void main(String[] args) {
        //获取Spring上下文对象
        ApplicationContext context = SpringApplication.run(IocDemoApplication.class, args);
        //从Spring上下文中获取对象
        UserController userController = context.getBean(UserController.class);
        //使用对象
        userController.sayHi();
        //根据类型获取bean
        UserService userService1 = context.getBean(UserService.class);
        userService1.sayHi();

        //根据名称name来获取bean
        UserService userService2 = (UserService) context.getBean("userService");
        userService2.sayHi();

        //根据名称+类型获取bean(name为类名小驼峰)
        UserService userService3 = context.getBean("userService", UserService.class);
        userService3.sayHi();

        //特殊情况(如果类名前两位大写,bean的名称即类名)
        AController aController = (AController) context.getBean("AController");
        aController.doController();

//        UserInfo UserInfo = context.getBean(UserInfo.class);
//        System.out.println(UserInfo);

        //根据bean名称, 从Spring上下⽂中获取对象
//        UserInfo userInfo1 = (UserInfo) context.getBean("user1");
//        UserInfo userInfo2 = (UserInfo) context.getBean("user2");
//        UserInfo userInfo3 = context.getBean("user1", UserInfo.class);//此时不需要强转
        UserInfo userInfo4 = (UserInfo) context.getBean("u1");
//        System.out.println(userInfo1);
//        System.out.println(userInfo2);
//        System.out.println(userInfo3);
        System.out.println(userInfo4);
    }

}
